export default {
  urlToRoute (url) {
    return this.toLine(url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.')))
  },
  urlTrans (url) {
    if (url.indexOf('http://') !== -1 && url.indexOf('/report/') !== -1) {
      return 'report' + url.substring(url.lastIndexOf('?'))
    }
    return url
  },
  // 驼峰转换下划线
  toLine (name) {
    let str = name.replace(/([A-Z])/g, '_$1').toLowerCase()
    if (str.indexOf('_') === 0) {
      str = str.substring(1)
    }
    return str
  },
  nvl (value) {
    return value || ''
  },
  strlen (str) {
    let len = 0
    for (let i = 0; i < str.length; i++) {
      let c = str.charCodeAt(i)
      // 单字节加1
      if ((c >= 0x0001 && c <= 0x007e) || (c >= 0xff60 && c <= 0xff9f)) {
        len++
      } else {
        len += 2
      }
    }
    return len
  },
  loadJs (url, callback) {
    let script = document.createElement('script')
    script.type = 'text/javascript'
    if (typeof (callback) !== 'undefined') {
      if (script.readyState) {
        script.onreadystatechange = () => {
          if (script.readyState === 'loaded' || script.readyState === 'complete') {
            script.onreadystatechange = null
            callback()
          }
        }
      } else {
        script.onload = () => {
          callback()
        }
      }
    }
    script.src = url
    document.body.appendChild(script)
  },
  getUrlRelativePath () {
    let url = document.location.toString()
    let arrUrl = url.split('//')

    let appName = process.env.BASE_URL

    let start = arrUrl[1].indexOf(appName)
    let relUrl = arrUrl[1].substring(start + appName.length)// stop省略，截取从start开始到结尾的所有字符

    if (relUrl.indexOf('?') !== -1) {
      relUrl = relUrl.split('?')[0]
    }
    return relUrl
  }

}
